package leetcode.code022;

import java.util.ArrayList;
import java.util.List;

public class Solution extends Solution022 {

	// n = 3
	// ["((()))","(()())","(())()","()(())","()()()"]

	private final char LK = '(';
	private final char RK = ')';

	@Override
	public List<String> generateParenthesis(int n) {
		List<String> ans = new ArrayList<>();
		char[] temp = new char[n * 2];
		generateParenthesis(n, n, temp, 0, ans);// n个左括号，n个右括号，从0开始， 组成一个temp，放到ans里。
		return ans;
	}

	private void generateParenthesis(int left, int right, char[] temp, int index, List<String> ans) {
		if (left == 0 && right == 0) {
			ans.add(new String(temp));
			return;
		}
		if (left > 0) {
			temp[index] = LK;
			this.generateParenthesis(left - 1, right, temp, index + 1, ans);
		}
		if (left < right) {
			temp[index] = RK;
			this.generateParenthesis(left, right - 1, temp, index + 1, ans);
		}
	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
	}

}
